﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_ModifyProductTaste : ASPXBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"].ToString() != "admin")
        {
            Response.Redirect("~/Admin/RedirectLogin.htm");
        }
        if (!IsPostBack)
        {
            BindRpt();
        }
    }

    /// <summary>
    /// 绑定Rpt
    /// </summary>
    void BindRpt()
    {
        int ProductId = Convert.ToInt16(GZ.Common.HttpHelper.GetParameter("Id", GZ.Common.HttpHelper.HttpType.GET));
        this.rptTasteList.DataSource = new GZ.Entity.Etaste().GetTastesByProductId(ProductId);
        this.rptTasteList.DataBind();
    }

    /// <summary>
    /// 保存事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        GZ.Entity.EProductInfo obj = new GZ.Entity.EProductInfo();
        GZ.Entity.Etaste objEtaste = new GZ.Entity.Etaste();

        obj.Id = Convert.ToInt16(GZ.Common.HttpHelper.GetParameter("Id", GZ.Common.HttpHelper.HttpType.GET));
        foreach (RepeaterItem item in this.rptTasteList.Items)
        {
            TextBox tb = (TextBox)item.FindControl("txtValue");
            HiddenField HId = item.FindControl("HId") as HiddenField;
            if (tb == null)
            {
                continue;
            }
            if (tb.Text.Length == 0)
            {
                continue;
            }
            if (Convert.ToInt16(tb.Text) <= 0)
            {
                continue;
            }

            objEtaste = new GZ.Entity.Etaste();
            objEtaste.Id = Convert.ToInt16(HId.Value);
            objEtaste.TasteValue = Convert.ToInt16(tb.Text);
            objEtaste.StartKey = "";
            objEtaste.TasteName = "";
            objEtaste.EndKey = "";
            obj.Taste.Add(objEtaste);

        }
        if (obj.UpdateTaste(obj))
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('保存成功');self.close();</script>");
        }
    }


    /// <summary>
    /// 行绑定时间
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rptTasteList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TextBox txtValue = e.Item.FindControl("txtValue") as TextBox;
            DataRowView drv = (DataRowView)e.Item.DataItem;
            txtValue.Text = drv["Value"].ToString();//获取当前行ID值 
            if (txtValue.Text.Length == 0)
            {
                txtValue.Text = "0";
            }
        }
    }
}